package april.example.keepalive;

import static org.jboss.netty.channel.Channels.pipeline;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.Timer;

public class ServerPipelineFactory implements ChannelPipelineFactory {

	private Timer timer;

	public ServerPipelineFactory(Timer timer) {
		this.timer = timer;
	}

	@Override
	public ChannelPipeline getPipeline() throws Exception {

		ChannelPipeline pipeline = pipeline();
		// 添加netty默认支持的 编解码器(可自动添加包头，并处理粘包问题)

		// pipeline.addLast("frameDecoder", new
		// ProtobufVarint32FrameDecoder());// 对应
		// pipeline.addLast("frameEncoder",
		// new ProtobufVarint32LengthFieldPrepender());// 此对象为
		// netty默认支持protocolbuf的编解码器
		pipeline.addLast("timeout", new IdleStateHandler(timer, 10, 10, 0));// 此两项为添加心跳机制
																			// 10秒查看一次在线的客户端channel是否空闲，IdleStateHandler为netty
																			// jar包中提供的类
		pipeline.addLast("hearbeat", new Heartbeat());// 此类
														// 实现了IdleStateAwareChannelHandler接口

		// netty会定时扫描 空闲的channel
		// pipeline.addLast("frameDecoder", new
		// ProtobufDecoder(Message.MessageInfo.getDefaultInstance()));
		// pipeline.addLast("frameEncoder", new ProtobufEncoder());//
		pipeline.addLast("handler", new ServerHandler());// 将编写好的服务器端的handler添加到这里

		return pipeline;
	}

}
